package com.hyny.framework.platform.system.config;


import com.hyny.framework.platform.system.filter.JwtAuthenticationTokenFilter;
import com.hyny.framework.platform.system.sucurity.CustomLogoutHandler;
import com.hyny.framework.platform.system.sucurity.CustomLogoutSuccessfulHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.LogoutFilter;

/**
 * SpringSecurity配置
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 放行请求地址
     */
//    public static final String REQUEST_RESOURCES_PERMIT_ALL = "/login,/exception,/test/*,/dataAuthority/info";
    public static final String []  REQUEST_RESOURCES_PERMIT_ALL = new String[]{"/login","/exception","/baseEnum/*","/fieldPower/*"};
//    public static final String []  REQUEST_RESOURCES_PERMIT_ALL = new String[]{"/sysMenu/list","/exception","/baseEnum/*","/fieldPower/*"};
    //方形静态文件地址
    public static final String [] ANT_MATCHERS=new String[]{"/doc.html","/favicon.ico", "/v2/api-docs", "/swagger-resources/**", "/webjars/**","/dataPower/**","/fieldPower/**"};


    /**
     * JWT过滤器
     */
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    /**
     * 自定义退出处理类
     */
    @Autowired
    private CustomLogoutHandler customLogoutHandler;

    /**
     * 自定义退出成功处理类
     */
    @Autowired
    private CustomLogoutSuccessfulHandler customLogoutSuccessfulHandler;

    /**
     * 密码配置
     *
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
//
    /**
     * 注入认证配置
     *
     * @return AuthenticationManager 认证器
     * @throws Exception 异常
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
//
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf().disable() // 关闭csrf
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 不通过Session获取SecurityContext
                .and().authorizeRequests()
                .antMatchers(REQUEST_RESOURCES_PERMIT_ALL).anonymous() // 允许匿名访问的接口
                .antMatchers(ANT_MATCHERS).permitAll()
                .anyRequest().authenticated() // 其余请求全部需要鉴权认证
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter, LogoutFilter.class).exceptionHandling() // 将自定义JWT过滤器添加到 LogoutFilter 之前，才能实现自定义登出
                .and()
                .logout().addLogoutHandler(customLogoutHandler).logoutSuccessHandler(customLogoutSuccessfulHandler); // 自定义登出

    }
}
